/***************************************************************************
 *   Copyright (C) 2008 by Goliash   *
 *   goliash@goliash.net   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "preferencesdialog.h"
#include "images.h"

#include <QFile>
#include <QFileInfoList>
#include <QDir>

#ifdef Q_OS_WIN
#include <windows.h>

bool isCDDevice(QString drive) {
	if (QSysInfo::WindowsVersion >= QSysInfo::WV_NT) {
		unsigned int r =  GetDriveTypeW((LPCWSTR) drive.utf16());
		qDebug("isCDDevice: '%s' r: %d", drive.toUtf8().data(), r);
		return (r == DRIVE_CDROM);
	} else {
		//Win98
		return true;
	}
}

#endif

PrefsDrivesWidget::PrefsDrivesWidget(QWidget * parent)
	: QWidget(parent)
{
	setupUi(this);

	// drive letters in Windows
#ifdef Q_OS_WIN
	QFileInfoList list = QDir::drives();
	for (int n = 0; n < list.size(); n++) {
		QString s = list[n].filePath();
		if (isCDDevice(s)) {
			if (s.endsWith("/")) s = s.remove( s.length()-1,1);
			device_combo->addItem( s );
		}
	}
	// /dev/* in others
#else
	QDir dev_dir("/dev");
	QStringList devices = dev_dir.entryList( QStringList() << "dvd*" << "cdrom*" << "cdrw*" << "sr*" << "cdrecorder*", 
                                             QDir::Files | QDir::System | QDir::Readable);
	for (int n=0; n < devices.count(); n++) {
		QString device_name = "/dev/" + devices[n];
		qDebug("PrefDrives::PrefDrives: device found: '%s'", device_name.toUtf8().constData());
		device_combo->addItem(device_name);
	}
#endif
	
	createHelp();
}

PrefsFiltersWidget::PrefsFiltersWidget(QWidget * parent)
	: QWidget(parent)
{
	setupUi(this);
	
	createHelp();
}

void PrefsDrivesWidget::setData(Preferences * prefs)
{
	setDevice(prefs->device);
}

void PrefsDrivesWidget::getData(Preferences * prefs)
{
	prefs->device = getDevice();
}

QString PrefsDrivesWidget::getDevice()
{
	return device_combo->currentText();
}

void PrefsDrivesWidget::setDevice(QString device)
{
	int i = device_combo->findText(device);
	if (i != -1)
		device_combo->setCurrentIndex(i);
	else if (device_combo->isEditable())
		device_combo->setEditText(device);
	else
		device_combo->setItemText(device_combo->currentIndex(), device);
}

void PrefsDrivesWidget::createHelp()
{
	QString text = tr("Choose your CD/DVD device. It will be set as default for adding of the new medium.");
	
	device_combo->setWhatsThis( text );
	device_combo->setToolTip( "<qt>"+ text +"</qt>" );
}

void PrefsFiltersWidget::createHelp()
{
	// set some tool tips

	/*QString text = tr("Choose your CD/DVD device. It will be set as default for adding of the new medium.");
	
	device_combo->setWhatsThis( text );
	device_combo->setToolTip( "<qt>"+ text +"</qt>" );*/
}

void PrefsFiltersWidget::setData(Preferences * prefs)
{
	// prepare thumbnail_size combobox
	thumbnail_size->insertItem(0, "90");
	thumbnail_size->insertItem(1, "120");
	thumbnail_size->insertItem(2, "150");

	// prepare tag_charset combobox
	QList<QByteArray> codecList = QTextCodec::availableCodecs();
	for(int i=0; i<codecList.size(); i++) {
		tag_charset->insertItem(i, codecList[i]);
	}
	
	useFilterJPEG(prefs->filter_JPEG);
	useFilterTxt(prefs->filter_txt);
	useFilterMp3(prefs->filter_mp3);
	useFilterPdf(prefs->filter_pdf);
	createThumbnail(prefs->thumbnail);
	lookForExif(prefs->exif);
	setThumbnailSize(prefs->thumbnail_size);
	setTagCharset(prefs->tag_charset);
}

void PrefsFiltersWidget::getData(Preferences * prefs)
{
	prefs->filter_JPEG = filterJPEG();
	prefs->filter_txt = filterTxt();
	prefs->filter_mp3 = filterMp3();
	prefs->filter_pdf = filterPdf();
	prefs->thumbnail = thumbnail();
	prefs->exif = exif();
	prefs->thumbnail_size = thumbnailSize();
	prefs->tag_charset = tagCharset();
}

void PrefsFiltersWidget::useFilterJPEG(bool b)
{
	checkBox_JPEG->setChecked(b);
}

bool PrefsFiltersWidget::filterJPEG()
{
	return checkBox_JPEG->isChecked();
}

void PrefsFiltersWidget::useFilterTxt(bool b)
{
	checkBox_txt->setChecked(b);
}

bool PrefsFiltersWidget::filterTxt()
{
	return checkBox_txt->isChecked();
}

void PrefsFiltersWidget::useFilterMp3(bool b)
{
	checkBox_mp3->setChecked(b);
}

bool PrefsFiltersWidget::filterMp3()
{
	return checkBox_mp3->isChecked();
}

void PrefsFiltersWidget::useFilterPdf(bool b)
{
	checkBox_pdf->setChecked(b);
}

bool PrefsFiltersWidget::filterPdf()
{
	return checkBox_pdf->isChecked();
}

void PrefsFiltersWidget::createThumbnail(bool b)
{
	checkBox_thumbnail->setChecked(b);
}

bool PrefsFiltersWidget::thumbnail()
{
	return checkBox_thumbnail->isChecked();
}

void PrefsFiltersWidget::lookForExif(bool b)
{
	checkBox_exif->setChecked(b);
}

bool PrefsFiltersWidget::exif()
{
	return checkBox_exif->isChecked();
}

void PrefsFiltersWidget::setThumbnailSize(int size)
{
	int index = thumbnail_size->findText(QString::number(size));
	if(index != -1) {
		thumbnail_size->setCurrentIndex(index);
	}
	else
		thumbnail_size->setCurrentIndex(0);
}

int PrefsFiltersWidget::thumbnailSize()
{
	return thumbnail_size->currentText().toInt();
}

void PrefsFiltersWidget::setTagCharset(QString charset)
{
	int index = tag_charset->findText(charset);
	if(index != -1) {
		tag_charset->setCurrentIndex(index);
	}
	else
		tag_charset->setCurrentIndex(0);
}

QString PrefsFiltersWidget::tagCharset()
{
	return tag_charset->currentText();
}

PreferencesDialog::PreferencesDialog(QWidget * parent)
	: QDialog(parent)
{
	setupUi(this);

	m_prefsDrives = new PrefsDrivesWidget(this);
	m_prefsFilters = new PrefsFiltersWidget(this);

	stackedWidget->addWidget(m_prefsDrives);
	stackedWidget->addWidget(m_prefsFilters);
	stackedWidget->setCurrentIndex(0);

	listWidget->addItem(new QListWidgetItem(Images::icon("cd"), m_prefsDrives->tabWidget->tabText(0), listWidget));
	listWidget->addItem(new QListWidgetItem(Images::icon("blockdevice"), tr("Filters"), listWidget));
	
	listWidget->setCurrentRow(0);

	m_prefsDrives->drive_icon->setPixmap(Images::icon("cd"));
	
	//connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), this, SLOT(restoreDefaults()));
 	//connect(buttonBox->button(QDialogButtonBox::Save), SIGNAL(clicked()), this, SLOT(saveSettings()));
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(saveSettings()));

	prefs = Preferences::instance();
	setData(prefs);

	// temporary disabled some filters:
	m_prefsFilters->checkBox_5->setDisabled(true);
	m_prefsFilters->checkBox_6->setDisabled(true);
	m_prefsFilters->checkBox_9->setDisabled(true);
}

bool PreferencesDialog::saveSettings()
{
	//qDebug() << "PreferencesDialog::saveSettings()";
	//Preferences * prefs = Preferences::instance();
	
	getData(prefs);
	prefs->save();	

	return true;
}

void PreferencesDialog::restoreDefaults()
{
	return;
}

void PreferencesDialog::getData(Preferences * pref)
{
	m_prefsDrives->getData(pref);
	m_prefsFilters->getData(pref);
}

void PreferencesDialog::setData(Preferences * pref)
{
	m_prefsDrives->setData(pref);
	m_prefsFilters->setData(pref);
}
